package methods;

import java.lang.reflect.Method;

public class MethodTableTest {
    public static void main(String[] args) throws ReflectiveOperationException {
        // 通过指定 方法名 和 参数类型 获取 平方 和 平方根 方法
        Method square = MethodTableTest.class.getMethod("square", double.class);
        Method sqrt = Math.class.getMethod("sqrt", double.class);

        // 调用方法
        printTable(1, 10, 10, square);
        printTable(1, 10, 10, sqrt);
    }

    public static double square(double x) {
        return x * x;
    }

    public static void printTable(double from, double to, int n, Method f) throws ReflectiveOperationException {
        // 打印方法信息
        System.out.println(f);

        double dx = (to - from) / (n - 1);

        for (double x = from; x <= to; x += dx) {
            double y = (Double) f.invoke(null, x);
            System.out.printf("%10.4f | %10.4f%n", x, y);
        }
    }
}
